import {Base} from "../models/base.js"
class Address extends Base{
  constructor(){
    super()
    this.url = "address"
  }

  getAddress(callback){
    var params = {
      url: this.url,
      fn: function (res) {
        callback && callback(res)
      }
    }
    this.request(params)
  }

  setAddress(data, callback){
    var params = {
      url: this.url,
      method: "POST",
      data: data,
      fn: (res) => {
        callback && callback(res)
      }
    }
    this.request(params)
  }

  getPostAddress(res){
    return {
      name: res.userName,
      mobile: res.telNumber,
      province: res.provinceName,
      city: res.cityName,
      country: res.countyName,
      detail: res.detailInfo
    }
  }


  chooseAddress(fn){
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.address']) {
          wx.authorize({
            scope: 'scope.address',
            success() {
              fn&&fn()
            }
          })
        }

        else {
          fn && fn()          
        }
      }
    })
  }

  updateDbAddress(res){
    var data = this.getPostAddress(res)
    this.setAddress(data, (res) => {
      if (this.processRes(res.statusCode)) {
        this.toast("成功更新地址")
      } else {
        this.toast("更新地址失败", "none")
      }
    })
  }
}

export {Address}